/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListTestPanel2.java
 *
 * Created on 20-06-2010, 17:34:57
 */
package gruppe1projekt.GUILayer;

import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JPanel;

/**
 *
 * @author Moulde
 */
public class SearchList<T> extends JPanel
{
    private ArrayList<T> Items;

    /** Creates new form ListTestPanel2 */
    public SearchList(ArrayList<T> Items)
    {
        initComponents();

        dlmItems = new DefaultListModel();
        listItems.setModel(dlmItems);

        this.Items = Items;

        InitializeDataModel();
    }
    
    private void InitializeDataModel()
    {
        FillDataModel(Items);
    }

    private void FillDataModel(ArrayList<T> Items)
    {
        dlmItems.clear();
        
        for (T item : Items)
        {
            dlmItems.add(dlmItems.size(), item);
        }

        lblCount.setText("Antal resultater: " + dlmItems.size());
    }

    public ArrayList<T> getItems()
    {
        return Items;
    }

    public void setItems(ArrayList<T> Items)
    {
        this.Items = Items;
        FillDataModel(Items);
    }

    public T GetSelectedItem()
    {
        int index = listItems.getSelectedIndex();
        if (index >= 0)
        {
            return (T)dlmItems.get(index);
        }
        else
        {
            return null;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listItems = new javax.swing.JList();
        txtSearch = new javax.swing.JTextField();
        lblSearchLabel = new javax.swing.JLabel();
        lblCount = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(229, 242));
        setMinimumSize(new java.awt.Dimension(229, 242));
        setPreferredSize(new java.awt.Dimension(229, 242));

        jScrollPane1.setViewportView(listItems);

        txtSearch.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txtSearchPropertyChange(evt);
            }
        });
        txtSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchKeyReleased(evt);
            }
        });

        lblSearchLabel.setText("Søgetekst:");

        lblCount.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lblSearchLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblCount, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                    .addComponent(txtSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                    .addComponent(lblSearchLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblCount)
                .addGap(12, 12, 12)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    DefaultListModel dlmItems;

    public void FilterList(String text)
    {
        ArrayList<T> filteredList = new ArrayList<T>();

        for (T item : Items)
        {
            if (item.toString().toLowerCase().contains(text.toLowerCase().subSequence(0, text.length())))
            {
                filteredList.add(item);
            }
        }

        FillDataModel(filteredList);
    }

    private void txtSearchPropertyChange(java.beans.PropertyChangeEvent evt)//GEN-FIRST:event_txtSearchPropertyChange
    {//GEN-HEADEREND:event_txtSearchPropertyChange

    }//GEN-LAST:event_txtSearchPropertyChange

    private void txtSearchKeyReleased(java.awt.event.KeyEvent evt)//GEN-FIRST:event_txtSearchKeyReleased
    {//GEN-HEADEREND:event_txtSearchKeyReleased
        FilterList(txtSearch.getText());
    }//GEN-LAST:event_txtSearchKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCount;
    private javax.swing.JLabel lblSearchLabel;
    private javax.swing.JList listItems;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
    }
